from presenter import basepresenter
from domain import comment

class PostCommentPresenter(basepresenter.BasePresenter):

    def respondToGet(self):
        if self.currentuserprofile:
            thetopic = self.discussiontask.getTopicByKey(self.request.get('topic'))
            self.template_values['topic'] = thetopic
            # set up the page title
            page_title = self.sitetitle + ' - New Reply'
            self.template_values['page_title'] = page_title
            #get the view to draw the page
            self.view.showGetResponse(self.template_values)
        else:
            self.view.redirectToUrl('/')

    def respondToPost(self):
        if self.currentuserprofile:
            # populate a comment object from the request
            newuser = self.currentuser
            newnickname = self.currentuserprofile.nickname
            newmessage = self.request.get('message')
            if self.request.get('topickey'):
                thetopic = self.discussiontask.getTopicByKey(self.request.get('topickey'))
                querystring='?topic=' + str(thetopic.key())
                newcomment = comment.Comment(message=newmessage, user=newuser, nickname=newnickname, topic=thetopic)
                newcomment.enabled = True
            # create the task and save the article
            self.discussiontask.saveComment(newcomment, self.currentuserprofile)
            #get the view to draw the page
            self.view.showPostResponse(self.template_values, querystring)
        else:
            self.view.redirectToUrl('/')     